package com.ymtms.arrivalofgoods.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ymtms.arrivalofgoods.constant.SmsConstants;
import com.ymtms.arrivalofgoods.dao.CallbackInformationMapper;
import com.ymtms.arrivalofgoods.dao.DeliveryGoodsMapper;
import com.ymtms.arrivalofgoods.dao.GoodsbillReceoptMainMapper;
import com.ymtms.arrivalofgoods.pojo.CallbackInformation;
import com.ymtms.arrivalofgoods.pojo.DeliveryGoods;
import com.ymtms.arrivalofgoods.pojo.GoodsbillReceoptMain;
import com.ymtms.arrivalofgoods.service.NotifyCustomerService;
import com.ymtms.arrivalofgoods.utils.CommonUtils;
import com.ymtms.arrivalofgoods.utils.Smsutil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class NotifyCustomerServiceImpl implements NotifyCustomerService {
    @Autowired
    private GoodsbillReceoptMainMapper goodsbillReceoptMainMapper;
    @Autowired
    private DeliveryGoodsMapper deliveryGoodsMapper;
    @Autowired
    private CallbackInformationMapper callbackInformationMapper;

    /**
     * Discription Todo通知客户取货
     *
     * @return
     * @Param num ，回执单主ID
     * @author Administrator
     * @date 2019/10/26 15:54
     **/
    @Override
    public void notifyCustomer(Integer num) {

        try {
        GoodsbillReceoptMain goodsbillReceoptMain = goodsbillReceoptMainMapper.selectByPrimaryKey(num);
        String phone = goodsbillReceoptMain.getLinkmanPhone();
        //动态取货码
        String code = SmsConstants.getCode();
        //判断同一商品发送信息的次数
        List<DeliveryGoods> deliveries = deliveryGoodsMapper.findAllDeliveryByGid(num);
        if (deliveries!=null&&deliveries.size() > 2) {
            throw new RuntimeException("已经通知过2次了");
        }
        //插入动态取货信息
        deliveryGoodsMapper.insertSelective(new DeliveryGoods(code, phone, num));
String myPhone="13308233567";

            //短信通知客户取货
            JSONObject modelParam = new JSONObject();
            modelParam.put("code", code);
            Smsutil.sendSms(SmsConstants.smsAppKey, SmsConstants.smsSerect, SmsConstants.smsSign, SmsConstants.smsModelId, myPhone, modelParam);
            //生成回告信息
            CallbackInformation callbackInformation = new CallbackInformation();
            callbackInformation.setDialNo(phone);
            callbackInformation.setType("到货");
            callbackInformation.setContent("请凭验证码：" + code + "到指定处取货物，有效时间24小时，请勿泄露给他人!!!!");
            callbackInformation.setWriteTime(new Date());
            callbackInformation.setWriter("接货员");
            callbackInformation.setFinallyDialTime(new Date());
            callbackInformation.setSuccess(1);
            callbackInformation.setLocked(0);
            callbackInformation.setBillId(CommonUtils.uuid());
            callbackInformation.setBillType("到货单");
            callbackInformation.setCount(1);
            callbackInformationMapper.insertSelective(callbackInformation);

        } catch (Exception e) {
            e.printStackTrace();
        }

   }


}
